package com.android.videotest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.android.htdvideoview.VideoControllerView;
import com.android.videotest.R;
import com.android.videotest.Urls;
import com.sansecy.android.simplevideo.Player;

import java.util.ArrayList;

public class DialogActivity extends AppCompatActivity {

    private Player mVideoView;

    public static void start(Context context) {
        Intent starter = new Intent(context, DialogActivity.class);
        context.startActivity(starter);
    }

    private int mPlayingIndex = -1;
    private static final String TAG = "DialogActivity-App";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        mVideoView = findViewById(R.id.videoView);
        ArrayList<String> strings = new ArrayList<>();
//        strings.add("http://www.sansecy.com/downloader/download/20191028104102_%E6%9D%A1%E7%A0%81%E7%A7%A4%E8%A7%84%E5%88%99.mp4");
        strings.add("http://video.htd.cn/sv/2e1d7394-16e6824c18c/2e1d7394-16e6824c18c.mp4");
//        mVideoView.setOnCompletionListener(new Player.OnCompletionListener() {
//            @Override
//            public void onCompletion() {
//                mPlayingIndex++;
//                if (mPlayingIndex >= strings.size()) {
//                    return;
//                }
//                String url = strings.get(mPlayingIndex);
//                mVideoView.loadUrl(Urls.cacheVideoUrl(url));
//                mVideoView.openVideo();
//            }
//        });

        mPlayingIndex = 0;
//        Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
        String url = strings.get(mPlayingIndex);
        mVideoView.loadUrl(Urls.cacheVideoUrl(Urls.videoUrl));
//        mVideoView.loadUri(uri);
        mVideoView.openVideo();
        VideoControllerView controllerView = new VideoControllerView(this);
        controllerView.attachToVideoView(mVideoView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mVideoView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVideoView.pause();
    }

    @Override
    protected void onDestroy() {
        mVideoView.stopPlayback();
        super.onDestroy();
    }

}
